﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MaxLeaf.Common;
using MaxLeaf.DB;

namespace MaxLeaf.DAL.K3
{
    /*
     * exec sp_executesql N'INSERT INTO t_Item (FName,FNumber,FItemClassID,FLevel,FParentID,FDetail,FFullNumber,FShortNumber) VALUES (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8)',N'@P1 varchar(80),@P2 varchar(80),@P3 int,@P4 int,@P5 int,@P6 int,@P7 varchar(80),@P8 varchar(80)'
     * ,'testunitgroup','testunitgroup',7,1,0,0,'testunitgroup','testunitgroup'
     * 
     * exec sp_executesql N'INSERT INTO t_UnitGroup (FUnitGroupID,FName) VALUES (@P1,@P2)',N'@P1 int,@P2 varchar(80)',9082,'testunitgroup'
     */

    /// <summary>
    /// 计量单位组
    /// </summary>
    public class t_UnitGroupDAL : BaseDAL
    {
        public t_UnitGroupDAL()
            : base()
        {
            strTableName = "t_UnitGroup";

            strMapKeyFieldName = "FUnitGroupID";
            strDestRealKeyFieldName = "FName";
            strFieldNames = "FName";
        }
        public string GetFUnitGroupID(DB.DBAccess dbOrgCon, DB.DBAccess dbDestCon, string strOrgFUnitGroupID)
        {
            if (strOrgFUnitGroupID.Equals("0") || strOrgFUnitGroupID.Length == 0)
            {
                return string.Empty;
            }
            DataRow drOrgData = dbOrgCon.GetTableDataFirstRow(strTableName, new Dictionary<string, object>(){
                    { "FUnitGroupID", strOrgFUnitGroupID }
                }
           );

            Update(dbOrgCon, dbDestCon, strOrgFUnitGroupID);

            DataTable dtData = dbDestCon.GetTableData(strTableName, new Dictionary<string, object>() { { "FName", drOrgData["FName"] } }, "FUnitGroupID");



            return CvtFunc.ToString(dtData.Rows[0]["FUnitGroupID"]);
        }

        protected override void DAL_GetFieldData(DBAccess dbOrgCon, DBAccess dbDestCon, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FUnitGroupID", StringComparison.InvariantCultureIgnoreCase))
            {

                drDestData[strFieldName] = GetFItemID(dbOrgCon, dbDestCon, "0", "0", CvtFunc.ToString(drOrgData["FName"]));

            }

            else
            {

                base.DAL_GetFieldData(dbOrgCon, dbDestCon, strFieldName, drOrgData, drDestData);
            }
        }

        private string GetFItemID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFParentID, string strFParentID, string strFnumber)
        {
            const string strFItemClassID = "7";
            const int intFDetail = 0;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL();


            strFItemId = dal.GetFItemId(dbOrgCon, dbDestCon, strFItemClassID, 1, strOrgFParentID, strFParentID, strFnumber, intFDetail);




            return strFItemId;
        }

        public DataRow Gett_UnitGroup(DBAccess dbCon, string strFUnitGroupID)
        {
            return dbCon.GetTableDataFirstRow(strTableName, new Dictionary<string, object>(){
                    { "FUnitGroupID", strFUnitGroupID }
                }
           );
        }

        public void SetDefaulunitid(DBAccess dbDestCon, string strDestFUnitGroupID, string strFUnitID)
        {
            string strsql = "update t_UnitGroup set FDefaultUnitID=@FDefaultUnitID  where FUnitGroupID=@FUnitGroupID";
            KeyValuePair<string, IDataParameter[]> kvp = dbDestCon.CreateCondParam(new Dictionary<string, object>() { { "FDefaultUnitID", strFUnitID }, { "FUnitGroupID", strDestFUnitGroupID } });

        }
    }
}
